﻿using System.Threading;
using XcoAppSpaces.Core.Ports;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XcoAppSpaces.Core;

namespace test.XcoAppSpaces.Core.Error_Reproductions
{
	[TestFixture]
	public class testDeadClientBlocksCommunication
	{
		[Serializable]
		public class Msg
		{
			public Port<string> ResponsePort { get; set; }
		}

		[Test]
		public void Test()
		{
			using (XcoAppSpace client1 = new XcoAppSpace("tcp.port=9001"),
			                   client2 = new XcoAppSpace("tcp.port=9002"),
			                   server = new XcoAppSpace("tcp.port=9000"))
			{
				server.Run<Msg>(msg => //simulate a server that does something and then posts a response
					{
						Thread.Sleep(50);
						msg.ResponsePort.Post("response");
					});

				var client1Worker = client1.Connect<Msg>("localhost:9000");
				for (int i = 0; i < 1000; i++)
				{
					client1Worker.Send(new Msg{ResponsePort = new Port<string>()});
				}

				client1.Dispose();

				Thread.Sleep(100);

				Console.WriteLine(DateTime.Now + " trying to connect to server...");
				var client2Worker = client2.Connect<Msg>("localhost:9000");
				Console.WriteLine(DateTime.Now + " connected!!");
			}
		}

		//[Test]
		//public void Test2()
		//{
		//	using (var client1 = new XcoAppSpace("tcp.port=9001"))
		//	{
		//		var client1Worker = client1.Connect<Msg>("localhost:9000");
		//	}
		//}
	}
}
